package controller

import (
	"api_interface/entity"
	"api_interface/service"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

//评论

func CreateComment() gin.HandlerFunc {
	return func(c *gin.Context) {
		var comment entity.CommentInfo               //用于传给service层的结构体
		if err := c.BindJSON(&comment); err != nil { //将body里的JSON格式数据解构到comment里
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to get json body",
				"data": nil,
			})
			return
		}
		currentTime := time.Now()
		timeString := currentTime.Format("2006-01-02 15:04:05") //定义时间存储样式
		comment.Tim = timeString                                //将时间赋值给comment.tim
		err := service.CreateComment(&comment)                  //调用service层函数，将comment传给函数用于创建comment
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to create comment",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //success
			"code": 0,
			"msg":  "success",
			"data": nil,
		})
	}
}

//点赞提问or取消点赞（完成）

type CLikeData struct {
	Aid int `json:"aid"`
	Cid int `json:"cid"`
}

func LikeCommentOrDeleteLike() gin.HandlerFunc {
	return func(c *gin.Context) {
		var CLikeD CLikeData
		if err := c.BindJSON(&CLikeD); err != nil { //接收点赞操作的信息
			c.JSON(http.StatusOK, gin.H{ //接受失败
				"code": 1,
				"msg":  "error to get json body",
				"data": nil,
			})
			return
		}
		err := service.LikeCommentOrDeleteLike(CLikeD.Aid, CLikeD.Cid) //调用service层函数进行操作
		if err != nil {
			c.JSON(http.StatusOK, gin.H{ //调用service层失败
				"code": 1,
				"msg":  "error to Like Or Delete Like",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //success
			"code": 0,
			"msg":  "success",
			"data": nil,
		})
	}
}

//获取评论

func GetComments() gin.HandlerFunc {
	return func(c *gin.Context) {
		qid1 := c.DefaultQuery("qid", "")             //qid
		page_num1 := c.DefaultQuery("pageNumber", "") //接收pageNum
		page_size1 := c.DefaultQuery("pageSize", "")  //接收pageSize
		aid1 := c.DefaultQuery("aid", "")             //接收aid
		qid, _ := strconv.Atoi(qid1)                  //将string转为int
		aid, _ := strconv.Atoi(aid1)
		page_num, _ := strconv.Atoi(page_num1)
		page_size, _ := strconv.Atoi(page_size1)
		commentswithliked, err := service.GetComments(aid, qid) //调用service层查看提问下的所有回复
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to get the comments",
				"data": nil,
			})
			return
		}
		length := len(*commentswithliked)
		var cutcommentswithliked []entity.CommentInfoWithLiked
		if (page_num+1)*page_size >= length {
			cutcommentswithliked = (*commentswithliked)[page_num*page_size : length]
		} else {
			cutcommentswithliked = (*commentswithliked)[page_num*page_size : (page_num+1)*page_size]
		}
		type CommentsData struct {
			Total    int                           `json:"total"`
			Comments []entity.CommentInfoWithLiked `json:"comments"`
		}
		data := CommentsData{
			Total:    length,
			Comments: cutcommentswithliked,
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "success",
			"data": data,
		})
	}
}
